package com.jbm.jbmsupplier.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.activity.repairs.UserAddress;
import com.jbm.jbmsupplier.net.NetAddRess;
import com.jbm.onehome.iface.ICertAddress;
import com.jbm.onehome.iface.ICertAddressCtrl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by donar_work on 2016/1/18.
 */
public class UserAddressAdapter extends BaseAdapter {
    private Context context = null;
    public ArrayList<UserAddress> mAddressList=null;
    private final int REMOVEADDRESS=0001;
    private final int ADDRESSLISTSHOW = 0002;
    private String category;
    public UserAddressAdapter(Context context,ArrayList<UserAddress> inList,String category)
    {
        this.context = context;
        this.mAddressList = inList;
        this.category=category;
    }
    @Override
    public int getCount() {
        return mAddressList.size();
    }
    @Override
    public Object getItem(int position) {
        return mAddressList.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }

    protected static boolean isDelete = false;
    //得到每一行的视图
    private Map<UserAddress,UserAddressCtrls> mInfoMaps = new HashMap<UserAddress,UserAddressCtrls>();
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(this.context).inflate(R.layout.address_item, null);
        final UserAddressCtrls ctrlsInfo = new UserAddressCtrls();
        ctrlsInfo.mAddressContext = (TextView) convertView.findViewById(R.id.id_address_context);
        ctrlsInfo.mDeleteButton = (ImageView) convertView.findViewById(R.id.id_address_delete);
        ctrlsInfo.mContacts = (TextView) convertView.findViewById(R.id.id_item_contacts);
        ctrlsInfo.mMobile = (TextView) convertView.findViewById(R.id.id_item_mobile);
        if (ctrlsInfo.mRSelectW != null)
            ctrlsInfo.mRSelectW.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Select(v);
                }
            });
        if (ctrlsInfo.mDeleteButton != null) {
            ctrlsInfo.mDeleteButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    isDelete = false;
                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setMessage("确认删除该地址吗？");
                    builder.setTitle("提示");
                    builder.setCancelable(false);
                    builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            UserAddress po = mAddressList.get(position);
                            NetAddRess netAddRess=new NetAddRess(mHandler);
                            netAddRess.RemoveAddress(po.id,REMOVEADDRESS);

                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            isDelete = false;
                            dialog.dismiss();
                        }
                    });
                    builder.create().show();
                }
            });
        }

        UserAddress info = mAddressList.get(position);
        if (info == null)
            return null;
        convertView.setTag(info);
        ctrlsInfo.mDeleteButton.setTag(info);
        try {
            String address = info.mAddress;
            ctrlsInfo.mAddressContext.setText(address.toCharArray(), 0, address.length());
            ctrlsInfo.mContacts.setText(info.mContacts.toCharArray(), 0, info.mContacts.length());
            ctrlsInfo.mMobile.setText(info.mMobile.toCharArray(), 0, info.mMobile.length());
            mInfoMaps.put(info, ctrlsInfo);
        } catch (Exception e)
        {
            String message = e.getMessage();
        }
        return convertView;
    }

    private void Select(View v) {
        UserAddress info = (UserAddress)v.getTag();
        if(info.isSelect == false) {
            for (int i = 0; i < mAddressList.size(); i++) {
                UserAddress item = mAddressList.get(i);
                if (item == info) {
                    item.isSelect = true;
                }
                else
                    item.isSelect = false;
            }
        }
        else
            info.isSelect = false;
        notifyDataSetChanged();
    }
    private Handler mHandler = new Handler() {
        public void handleMessage(Message paramMessage) {
            switch (paramMessage.what) {
                case REMOVEADDRESS:{
                    ICertAddress address =(ICertAddress)paramMessage.obj;
                    if (address.m_code==200){
                        NetAddRess netAddRess = new NetAddRess(mHandler);
                        netAddRess.AddressMessage(ADDRESSLISTSHOW,category);
                        Toast.makeText(context, "亲，地址删除成功", Toast.LENGTH_SHORT).show();
                    }
                    break;
                }
                case  ADDRESSLISTSHOW:
                    ICertAddressCtrl ctrl = (ICertAddressCtrl)paramMessage.obj;
                    if (ctrl.m_code==200) {
                        mAddressList.clear();
                        if (ctrl.CAlist!=null){
                            for (int i = 0; i < ctrl.CAlist.length; i++) {
                                ICertAddress key = ctrl.IndexofList(i);
                                if (key.cert_status.equals("uncert")) {
                                    if (key.gi_id == 0) {
                                        ICertAddress item = ctrl.CAlist[i];
                                        UserAddress address = new UserAddress();
                                        address.mAddress = item.gi_text;
                                        address.id = item.caddr_id;
                                        address.mContacts = item.contacts;
                                        address.mMobile = item.mobile;
                                  mAddressList.add(address);
                                        notifyDataSetChanged();
                     }}}}
                    else{
                            notifyDataSetChanged();
                        }
                    }

                        break;


            }
        }
    };

}

class UserAddressCtrls{
    FrameLayout mRSelectW = null;
    ImageView mReadioSelect =null;
    TextView mContacts = null;
    TextView mMobile = null;
    TextView mAddressContext = null;
    ImageView mDeleteButton = null;
}
